package com.hdsx.validator.validate.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;

public class XMLParser {
	/**
	 * 得到document
	 * @param xmlPath
	 * @return
	 */
	public static Document getDocument(String filePath)
	{
		SAXReader reader=new SAXReader();
		Document document = null;
		try {
			document = reader.read(new File(filePath.replace("\\","/")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}
	public static Document getDocument(InputStream inputStream)
	{
		SAXReader reader=new SAXReader();
		Document document = null;
		try {
			document = reader.read(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}
	@SuppressWarnings("unchecked")
	public static List<String> getValues(Document document,String xPath)
	{
		XPath selector=getXPath(document, xPath);
		List<Element> nodes=selector.selectNodes(document);
		List<String> list=new ArrayList<String>();
		for (Element element : nodes) {
			list.add(element.getText().trim());
		}
		return list;
	}
	public static String getSingleValue(Document document,String xPath)
	{
		XPath selector=getXPath(document, xPath);
		Element element= (Element) selector.selectSingleNode(document);
		return element.getText();
	}
	@SuppressWarnings("unchecked")
	public static List<String> getAttributes(Document document,String xPath,String attribute)
	{
		XPath selector=getXPath(document, xPath);
		List<Element> nodes=selector.selectNodes(document);
		List<String> list=new ArrayList<String>();
		for (Element element : nodes) {
			list.add(element.attributeValue(attribute));
		}
		return list;
	}
	public static boolean validXml(String xsdPath,String xmlPath)
	{
		SAXReader reader = new SAXReader();  

		reader.setValidation(true);  
		try {  
			reader.setFeature("http://xml.org/sax/features/validation", true);  
			reader.setFeature("http://apache.org/xml/features/validation/schema", true);  
			reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",xsdPath);  
			XMLErrorHandler errorHandler = new XMLErrorHandler();  
			reader.setErrorHandler(errorHandler);  

			XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());  
			if (errorHandler.getErrors().hasContent()) {  
				writer.write(errorHandler.getErrors()); 
				return false;
			} else {  
				return true;
			}  
		} catch (Exception e) {  
			// Logging.INFO(XMLParser.class,"xml校验失败,失败原因:{}",e.getMessage()); 
			return false;
		}  

	}
	private static XPath getXPath(Document doc,String xPath)
	{
		Element root=doc.getRootElement();
		String namespace=root.getNamespaceURI();
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("default",namespace);
		XPath selector=doc.createXPath("//default:"+xPath);
		selector.setNamespaceURIs(map);
		return selector;
	}
	public static void main(String[] args) {
		Document doc;
		try {
			doc = XMLParser.getDocument(new FileInputStream(new File("D:/test/DefaultValidate.xml")));
			//List<String> list=XMLParser.getValues(doc, "validators/item");
			List<String> alias=XMLParser.getAttributes(doc,"validators/item", "alias");
			System.out.println(alias);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
